package com.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import javax.naming.NamingException;

import com.domain.PackagesInfo;

public class PackagesInfoDAO extends BaseDAO {
	public int insert(PackagesInfo packageInfo) {
		int result = 0;
		try {
			Connection conn = getConnection();
			String sql = "insert into packageInfos values(?, ?, ?, ?, ?, ?)";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, packageInfo.getPackageId());
			stmt.setString(2, packageInfo.getPackageName());
			stmt.setFloat(3, packageInfo.getPrice());
			stmt.setDate(4, packageInfo.getStartDate());
			stmt.setDate(5, packageInfo.getEndDate());
			stmt.setString(6, packageInfo.getDescription());
			result = stmt.executeUpdate();
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public List<PackagesInfo> getAllPackageInfo() {
		List<PackagesInfo> list = new LinkedList<PackagesInfo>();
		try {
			Connection conn = getConnection();
			String sql = "select * from packageInfos";
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String packageId = rs.getString("packageId");
				String packageName = rs.getString("packageName");
				float price = rs.getFloat("price");
				Date startDate = rs.getDate("startDate");
				Date endDate = rs.getDate("endDate");
				String description = rs.getString("description");
				list.add(new PackagesInfo(packageId, packageName, price,
						startDate, endDate, description));
			}
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public void deletePackageInfoByPackageId(String code) {
		try {
			Connection conn = getConnection();
			String sql = "delete from packageinfos where packageid=?";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, code);
			stmt.executeUpdate();			
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public PackagesInfo getPackageInfoById(String code) {
		PackagesInfo packageInfo = null;
		try {
			Connection conn = getConnection();
			String sql = "select * from packageInfos where packageId=?";
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, code);
			ResultSet rs = stmt.executeQuery();
			rs.next();
			if (!rs.wasNull()) {
				String packageId = rs.getString("packageId");
				String packageName = rs.getString("packageName");
				float price = rs.getFloat("price");
				Date startDate = rs.getDate("startDate");
				Date endDate = rs.getDate("endDate");
				String description = rs.getString("description");
				packageInfo = new PackagesInfo(packageId, packageName, price,
						startDate, endDate, description);
			}
			conn.close();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return packageInfo;
	}
}
